<template>
	<view class="content">
		<view class="informationBox">
			<view class="myli">
				<label>头像</label>
				<view class="rightInfo">
					<button open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
						<image :src="userInfo.headerImage" mode=""></image>
					</button>
				</view>
			</view>
			<view class="myli">
				<label>昵称</label>
				<view class="rightInfo" @click="showNick">
					{{userInfo.nickName}}
					<view class="rightImg">
						
					</view>
				</view>
			</view>
			<view class="myli">
				<label>手机号码</label>
				<view class="rightInfo">
					{{userInfo.phone}}
				</view>
			</view>
		</view>
		
		<view class="informationBox" style="margin-top:15rpx;">
			<view class="myli" style="display: inherit;" @click="logout">
				<label style="text-align: center;color:#333;">退出登录</label>
			</view>
		</view>
		
		<updateInfo ref="updateInfo" @showUseInfo="showUser"></updateInfo>
		
	</view>
</template>

<script>
	import updateInfo from '@/components/updateInfo.vue';
	import baseUrl  from '@/util/config.js';
	import { getUserInfoDetail, updateUserInfo } from '@/api/login.js';
	export default {
		components:{
			updateInfo
		},
		data() {
			return {
				userInfo: {}
			}
		},
		onLoad() {
			this.showUser();
		},
		methods: {
			showUser() {
				if(uni.getStorageSync('isChange') == 1) {
					this.userInfo = uni.getStorageSync('userInfo');
					return;
				}
				if(uni.getStorageSync('isLogout') == 1) {
					this.userInfo = uni.getStorageSync('userInfo');
					return;
				}
				getUserInfoDetail({id: uni.getStorageSync('userInfo').id}).then(res=>{
					if(res.code == 200) {
						this.userInfo = res.data;
						uni.setStorageSync('userInfo',res.data);
					} else {
						uni.$u.toast(res.msg);
					}
				})
			},
			onChooseAvatar(e){
				uni.uploadFile({
					url: baseUrl.baseUrl + '/common/upload',
					filePath: e.detail.avatarUrl,
					name: 'file',
					header: {
						Authorization: 'Bearer ' + uni.getStorageSync('userToken')
					},
					formData: {
						frontFileType: 'picture'
					},
					success: res => {
						var jsurl = JSON.parse(res.data)
						if (jsurl.code != 200) {
							return uni.$u.toast(jsurl.msg);
						}
						this.userInfo.headerImage = jsurl.url;
						const pmm = {
							id: uni.getStorageSync('userInfo').id,
							headerImage: jsurl.url,
							nickName: uni.getStorageSync('userInfo').nickName
						};
						updateUserInfo(pmm).then(res=>{
							if(res.code == 200) {
								this.showUser();
							} else {
								uni.$u.toast(res.msg);
							}
						})
						
					}
				});	
			},
			showNick(){
				this.$refs['updateInfo'].showInfo();
			},
			// 退出登录
			logout(){
				uni.showModal({
				    title: '提示',
				    content: '确定要退出登录吗?', 
				    success: res => {
				        if (res.confirm) {
							uni.setStorageSync('isLogout',1);
				            uni.removeStorageSync('userToken');
							// uni.removeStorageSync('needReg');
							// uni.removeStorageSync('openId');
							// uni.removeStorageSync('userInfo');
							console.log(uni.getStorageSync('userInfo'),'===4===444');
							if(uni.getStorageSync('userInfo').isMaster == 1) {
								// uni.reLaunch({
								// 	url: '/pages/engineer/mine'
								// });
								uni.reLaunch({
									url: '/pages/mine/index'
								});
							} else {
								uni.reLaunch({
									url: '/pages/mine/index'
								});
							}
				        } else if (res.cancel) {
					
				        }
				    }
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
.content{
	background-color:#f5f5f5;min-height: 100vh;padding:24rpx 32rpx 0;
	.informationBox{
		background-color:#fff;border-radius:12rpx;box-shadow: 0px -1px 0px 0px #F0F0F0 inset;
		.myli{
			@include flx;justify-content: space-between;padding:0 32rpx;border-bottom:1px solid #F0F0F0;
			label{
				display: block;@include baseFont(30rpx,#666,112rpx);
			}
			.rightInfo{
				@include baseFont(30rpx,#333,112rpx);@include flx;align-items: center;
				image{
					display: block;width:88rpx;height:88rpx;border-radius:50%;
				}
				.rightImg{
					width:24rpx;height:26rpx;background: url('../../static/nameRight.png') no-repeat;background-size:24rpx 26rpx;
					margin-left:12rpx;
				}
			}
		}
	}
}
</style>
